package com.zhouyu.proxyFactoryBean;

import com.zhouyu.service.UserService;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

//@Component
public class proxyFacoryBean {

	@Bean
	public ProxyFactoryBean userServiceProxy(){
		UserService userService = new UserService();

		ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
		//普通方式
/*		proxyFactoryBean.setTarget(userService);
		proxyFactoryBean.addAdvice(new MethodInterceptor() {
			@Override
			public Object invoke(MethodInvocation invocation) throws Throwable {
				System.out.println("before...");
				Object result = invocation.proceed();
				System.out.println("after...");
				return result;
			}
		});*/

		//指定其他的Advice
		proxyFactoryBean.setTarget(userService);
		proxyFactoryBean.setInterceptorNames("zhouyuAroundAdvise");

		return proxyFactoryBean;
	}



	@Bean
	public MethodInterceptor zhouyuAroundAdvise(){
		return new MethodInterceptor() {
			@Override
			public Object invoke(MethodInvocation invocation) throws Throwable {
				System.out.println("before...");
				Object result = invocation.proceed();
				System.out.println("after...");
				return result;
			}
		};
	}


}
